<template>
    <div>
        <!--面包屑导航区域-->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/buyerIndex' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>个人信息</el-breadcrumb-item>
        </el-breadcrumb>

        <!--卡片视图区域-->
        <el-card>
            <!--搜索与添加区域-->
            <el-row :gutter="20">
                <el-col :span="7">
                    <el-input placeholder="请输入查询信息" v-model="queryInfo.query" clearable
                              @clear="getApplyInformationList">
                        <el-button slot="append" icon="el-icon-search" @click="getApplyInformationList"></el-button>
                    </el-input>
                </el-col>
            </el-row>

            <!--采购列表区域-->
            <el-table :data="applyInformationList" border stripe>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="货物名称">
                    <template slot-scope="scope">
                        <el-tag size="medium">{{ scope.row.goodsName }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="货物数量" prop="goodsNum"></el-table-column>
                <el-table-column label="采购状态" prop="status"
                                 :filters="[{ text: '待处理', value: '待处理' }, { text: '已接单', value: '已接单' }, { text: '待发货', value: '待发货' }, { text: '待签收', value: '待签收' }, { text: '已完成', value: '已完成' }, { text: '退货中', value: '退货中' }]"
                                 :filter-method="filterStatus">
                    <template slot-scope="scope">
                        <el-tag type="danger" v-if="scope.row.status === '退货中'">退货中</el-tag>
                        <el-tag type="danger" v-if="scope.row.status === '待处理'">待处理</el-tag>
                        <el-tag type="info" v-if="scope.row.status === '已接单'">已接单</el-tag>
                        <el-tag type="warning" v-if="scope.row.status === '待发货'">待发货</el-tag>
                        <el-tag v-if="scope.row.status === '待签收'">待签收</el-tag>
                        <el-tag type="success" v-if="scope.row.status === '已完成'">已完成</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="上报者姓名">
                    <template slot-scope="scope">
                        <el-tag size="medium">{{ scope.row.submitterName }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="上报者身份">
                    <template slot-scope="scope">
                        <el-tag type="success" size="medium">{{ scope.row.submitterIdentity }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="上报者联系方式" prop="submitterContact"></el-table-column>
                <el-table-column label="采购员姓名" prop="buyerName"></el-table-column>
                <el-table-column label="采购员联系方式" prop="buyerContact"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-tooltip effect="dark" content="接单" placement="top" :enterable="false">
                            <el-button type="primary" size="mini" icon="el-icon-setting" v-if="scope.row.buyerId < 1"
                                       @click="toBuy(scope.row.applyInformationId)"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>

            <!--分页区域-->
            <el-pagination
                    background
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="queryInfo.current"
                    :page-sizes="[2, 3, 5, 10]"
                    :page-size="queryInfo.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </el-card>
    </div>
</template>

<script scoped>
    export default {
        data() {
            return {
                queryInfo: {
                    buyerId: '',
                    query: '',
                    // 当前的页数
                    current: 1,
                    // 当前每页显示多少条数据
                    pageSize: 7
                },
                applyInformationList: [],
                total: 0,
                token: '',
            }
        },
        async created() {
            this.token = window.sessionStorage.getItem("token")
            var buyer = await this.$http.get('/user/buyer/getInformationFromToken', {
                params: {
                    token: this.token
                }
            })
            this.queryInfo.buyerId = buyer.data.data.buyerId
            this.getApplyInformationList()
        },
        methods: {
            async getApplyInformationList() {
                var result = await this.$http.get('user/buyer/selectApplyInformation', {params: this.queryInfo})
                if (result.data.status !== 200) {
                    return this.$message.error("获取采购信息列表失败！")
                }
                this.applyInformationList = result.data.data.records
                this.total = result.data.data.total
            },
            //监听pagesize改变的事件
            handleSizeChange(newSize) {
                this.queryInfo.pageSize = newSize
                this.getApplyInformationList()
            },
            //监听页码值改变的事件
            handleCurrentChange(newPage) {
                this.queryInfo.current = newPage
                this.getApplyInformationList()
            },
            async toBuy(applyInformationId) {
                var result = await this.$http.get('user/buyer/updateApplyInformation', {
                    params: {
                        buyerId: this.queryInfo.buyerId,
                        applyInformationId: applyInformationId
                    }
                })
                if (result.data.status !== 200) {
                    return this.$message.error("请完善个人信息！！！")
                }
                this.$message.success("更新成功！")
                this.getApplyInformationList()
            },
            filterStatus(value, row) {
                return row.status === value;
            }
        }
    }
</script>

<style scoped>
    .el-breadcrumb {
        margin-bottom: 15px;
        font-size: 12px;
    }

    .el-card {
        box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
    }

    .el-table {
        margin-top: 15px;
        font-size: 12px;
    }

    .el-pagination {
        text-align: right;
        margin-top: 15px;
    }

</style>
